package com.lzh.dlykserver.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.config.listener.UploadDataListener;
import com.lzh.dlykserver.constant.Constants;
import com.lzh.dlykserver.entity.TClue;
import com.lzh.dlykserver.mapper.TClueMapper;
import com.lzh.dlykserver.query.BaseQuery;
import com.lzh.dlykserver.query.ClueQuery;
import com.lzh.dlykserver.service.Clueservice;
import com.lzh.dlykserver.util.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ClueserviceImpl implements Clueservice {

    @Resource
    private TClueMapper tClueMapper;

    @Override
    public PageInfo<TClue> getClueByPage(Integer current) {
        //设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TClue> list = tClueMapper.selectUserByPage(BaseQuery.builder().build());
        //封装分页数据到PageInfo
        PageInfo<TClue> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void importExcel(InputStream inputStream, String token) {
        //链式编程，三个参数，第一个参数为要读取的excel文件，第二个是excel模板类，第三个参数是监听器
        EasyExcel.read(inputStream, TClue.class, new UploadDataListener(tClueMapper, token))
                .sheet()
                .doRead();
    }

    @Override
    public Boolean checkPhone(String phone) {
        int count = tClueMapper.selectByCount(phone);
        return count <= 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();

        BeanUtils.copyProperties(clueQuery, tClue);

        Integer loginUserId = JWTUtils.parseUserFromJWT(clueQuery.getToken()).getId();

        tClue.setCreateBy(loginUserId);
        tClue.setCreateTime(new Date());

        return tClueMapper.insertSelective(tClue);
    }

    @Override
    public TClue getById(Integer id) {
        return tClueMapper.selectDetailById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();

        BeanUtils.copyProperties(clueQuery, tClue);

        Integer loginUserId = JWTUtils.parseUserFromJWT(clueQuery.getToken()).getId();

        tClue.setEditBy(loginUserId);
        tClue.setEditTime(new Date());

        return tClueMapper.updateByPrimaryKeySelective(tClue);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteClue(Integer id) {
        return tClueMapper.deleteByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDeleteClue(List<String> idList) {
        return tClueMapper.delByIds(idList);
    }
}
